<?php
/**
 * _smuoiAppInterface is the GUI base for applications
 * 
 * @package Smernel
 */
 
	class _smuoiAppInterface extends _smuoiUserInterface{
	
		/**
		 * Package this interface belongs to
		 * @var _smuoiPackage
		 */
		var $pkg;
		
		/**
		* arguments the application was called with
		* @deprecated
		* @var array
		*/
		
		/**
		* member functions which are callable via ajax
		* @var array
		*/
		protected $ajax_methods = array();

		/**
		* ajax method called
		* @var string
		*/
		protected $ajax_method;
		
		/**
		* args sent to ajax method
		* @var array
		*/
		protected $ajax_args;
		
		/**
		* gui toolbar, if any
		* @var Toolbar
		*/
		var $toolbar;
	
		function __construct($pkg=false, $args=null){
			$this->pkg = $pkg;
			$this->args = ($args instanceof args) ? $args : new args($args);
			if(is_dir($privat_dir = $this->incpath('Resources/@public'))) $this->public_resources_link($privat_dir, DSK_ROOT.'/@public/Apps/'.$this->modpath());
			if(DOAJAX) return;
			Screen()->inc_css('chrome.css');
			js_include_shared('smototype.js', 'Smuoi.js', 'SMRDE.js', 'scriptaculous.js?load=effects'); 
		}
		
		/**
		* uses HTTP error to fail a method call
		* @deprecated
		* @param integer status code
		* @param string message
		*/
		function failE($e, $m=''){
			httpE($e, $m);
		}
		
		/**
		* path to a file under this package
		* @param string filepath
		* @returns string
		*/
		function incpath($file){
			return realpath(PKG_DIR.'/'.$this->modpath().'/'.$file);
		}
		
		/**
		* path to package below Apps dir
		* @deprecated
		* @returns string
		*/
		function modpath(){
			return $this->pkg->path;	
		}
		
		/**
		* full uri to public resource
		* @param string filename
		* @param string subdirectory
		* @param string path to package
		* @returns string
		*/
		function file_uri($filename, $sub=false, $path=false){
			if(!$path) $path = $this->modpath();
			return SHR."/Apps/$path$sub/$filename";
		}
		
		/**
		* retrieves argument interface was instantiated with
		* @deprecated
		* @param string arg name
		* @returns mixed
		*/
		function arg($k){
			return $this->args->$k;
		}
		
		/**
		* retrieves all arguments interface was instantiated with
		* @deprecated
		* @returns object
		*/
		function args(){
			return $this->args;
		}
		
		/**
		* instantiates Toolbar
		* @returns Toolbar
		*/
		function Toolbar(){
			if(is_null($this->toolbar)){
				require_once 'Toolbar.php';
				$Tlb = new Tlb;
				$Tlb->option('Smuoi')->option('Switch User')->toFunction('TUX.say("authenticate");');
				$Tlb->option('Smuoi')->option('Get Public Key')->toFunction('TUX.say("pubkey");');
				$this->toolbar = $Tlb;
			}
			return $this->toolbar;
		}
		
		/**
		* builds navigation element
		* @returns string
		*/
		function build_nav_element(){
			$a = explode('/', ARF);
			if(!$last = array_pop($a)) return 'desk: /';
			$href = '';
			$nav = '<a href="/">desk:</a> / ';
			foreach($a as $i=>$h){
				$href .= "/$h";
				$nav .= '<a href="'.$href.'">'.$h.'</a> / ';
			}
			$nav .= $last;
			return $nav;
		}
		
		/**
		* full uri to icon
		* @param string filename
		* @param integer size
		* @returns string
		*/
		function icon($name=false, $size='128'){
			if(!$name) $name = $this->arg('icon');
			if(substr($name, 0, 7) == 'http://') return $name;
			return $this->file_uri($name, "/images/$size");
		}
		
####	AJAX METHODS
		
		/**
		* runs ajax
		*/
		function run_ajax(){
			$this->ajax_method = post('f');
			$this->ajax_args = ($a = post('a')) ? object(json_decode($a)) : object();
			// check method requested is kosher
			if(!in_array($this->ajax_method, $this->ajax_methods)) httpE(400, get_class($this).": Method '$this->ajax_method' not registered here.");
			// Child classes can plugin an intervening function here
			if(!$this->setup_ajax($this->ajax_args)) return;
			// Call the requested method
			$func = $this->ajax_method;
			$this->$func($this->ajax_args);
			// setting callback in request object means simply appending it as string to the reply
			if(isset($this->ajax_args->callback)) jsln($this->ajax_args->callback);
			// ...and out
			OB()->flush();
		}
		
		/**
		* For security, overwritable by child classes
		*/
		function setup_ajax($o){
			return true;
		}	
		
	}

